package cn.lzm.org.controller;

import cn.lzm.basic.util.AjaxResult;
import cn.lzm.basic.util.PageList;
import cn.lzm.org.domain.Employee;

import cn.lzm.org.query.EmployeeQuery;
import cn.lzm.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {


    @Autowired
    private IEmployeeService employeeService;

    /**
     * 添加/修改
     */
    @PostMapping("/addorup")
    @Transactional
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        if (null == employee.getId()){
            // 添加
            employeeService.add(employee);
        }else {
            // 修改
            employeeService.update(employee);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除 delete localhost:8080/dept/1
     */
    @DeleteMapping("/{id}")
    @Transactional
    public AjaxResult delete(@PathVariable("id") Long id){
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 查询所有
     */
    @GetMapping
    public List<Employee> findAll(){
        return employeeService.queryAll();
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }

    /**
     *
     * 分页查询
     */
    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        System.out.println(query);
        System.out.println(query.getCurrentPage());
        System.out.println(query.getPageSize());
        return employeeService.queryByPage(query);
    }
    /*批量删除*/
    @PatchMapping("/batchDel")
    @Transactional
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        System.out.println(ids);
        employeeService.batchDel(ids);
        return AjaxResult.me().success();
    }

    /**
     * 根据部门id查询 部门员工
     */
    @GetMapping("/deptId/{departmentId}")
    public List<Employee> findByDepartmentId(@PathVariable("departmentId") Long departmentId){
        return employeeService.findByDepartmentId(departmentId);
    }

}

